﻿using System;

using System.IdentityModel.Tokens;

namespace AMT.IdentityModel.OAuth
{
	// TODO: is there a better class for inherit?  something that already has username & key?
	/// <summary>
	/// Represents a Simple Web Token identity claim based on user name and signing key
	/// </summary>
	public class SWTUserSymmetricKeyClaimToken : SecurityToken
	{
		private const int SYMMETRIC_KEY_BYTE_COUNT = 32;

		#region Read-only public properties
		public string UserName { get; internal set; }
		public byte[] SymmetricKey { get; internal set; }
		#endregion

		#region Public constructors
		/// <summary>
		/// Constructs an instance of this class from user name and signing key
		/// </summary>
		/// <param name="userName">The user name associated with the signing key</param>
		/// <param name="symmetricKey">The signing key assigned to the user name</param>
		public SWTUserSymmetricKeyClaimToken(string userName, byte[] symmetricKey)
		{
			if (null == userName) throw new ArgumentNullException("userName");
			if (string.IsNullOrEmpty(userName)) throw new ArgumentException("Invalid: userName is empty");
			if (null == symmetricKey) throw new ArgumentNullException("symmetricKey");
			if (SYMMETRIC_KEY_BYTE_COUNT != symmetricKey.Length) throw new ArgumentException("Invalid: Size of symmetricKey");

			UserName = userName;
			SymmetricKey = symmetricKey;
		}

		/// <summary>
		/// Constructs an instance of this class from user name and signing key
		/// </summary>
		/// <param name="userName">The user name associated with the signing key</param>
		/// <param name="symmetricKeyBase64">The signing key, in Base64 format, assigned to the user name</param>
		public SWTUserSymmetricKeyClaimToken(string userName, string symmetricKeyBase64)
			: this(userName, Convert.FromBase64String(symmetricKeyBase64))
		{
		}
		#endregion

		#region Invalid or unimplemented SecurityToken methods
		public override string Id
		{
			get { throw new NotImplementedException(); }
		}

		public override System.Collections.ObjectModel.ReadOnlyCollection<SecurityKey> SecurityKeys
		{
			get { throw new NotImplementedException(); }
		}

		public override DateTime ValidFrom
		{
			get { throw new NotImplementedException(); }
		}

		public override DateTime ValidTo
		{
			get { throw new NotImplementedException(); }
		}
		#endregion
	}
}
